﻿using System.Collections.Generic;
using UnityEngine;

public class CreatorCamera : ICreator<PropertyCamera> {
	// === Public =====================================================================================================
	public static CreatorCamera CreateInstance() {
		var instance = new CreatorCamera();
		return instance;
	}

	#region ICreator
	public void Create(PropertyCamera properties) {
		var camera = Tools.GameObject.Create(ResourceConstants.Prefabs.MAIN_CAMERA);
		Init(camera, properties);
		_baseData.SetCamera(camera);
	}

	public void DestroyAll() {
		Destroy(_baseData.Camera);
	}

	public void Destroy(List<GameObject> goList) {
		foreach (var gameObject in goList) {
			Destroy(gameObject);
		}
	}

	public void Destroy(GameObject go) {
		if (!_baseData.HasCamera()) {
			return;
		}
		if (!go.Equals(_baseData.Camera)) {
			return;
		}
		Object.Destroy(go);
		_baseData.SetCamera(null);
	}
	#endregion

	// === Private ====================================================================================================
	private readonly MapEditorBaseData _baseData = MapEditorBaseData.GetInstance;

	private void Init(GameObject go, PropertyCamera properties) {
		go.transform.position = properties.Position;
		go.transform.rotation = properties.Rotation;
		go.camera.orthographic = properties.IsOrthoGraphic;
		go.camera.orthographicSize = properties.OrthoGraphicSize;
		go.camera.nearClipPlane = properties.NearClipPlane;
		go.camera.farClipPlane = properties.FarClipPlane;
		foreach (var component in properties.Components) {
			AddComponent(go, component);
		}
	}

	private void AddComponent(GameObject go, string className) {
		go.AddComponent(className);
	}
}
